
package com.lyyzoo.gpss.web.base;

import com.google.common.collect.Maps;
import com.lyyzoo.bean.Result;
import com.lyyzoo.util.Results;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

public class BaseController {
    public BaseController() {
    }

    public Result SUCCESS() {
        return Results.success();
    }

    public Result SUCCESS(String msg) {
        return Results.success(msg);
    }

    public Result SUCCESS(Object data) {
        return Results.successWithData(data);
    }

    public Result SUCCESS(Object data, String msg) {
        return Results.successWithData(data, msg);
    }

    public Result FAILURE() {
        return Results.failure();
    }

    public Result FAILURE(String msg) {
        return Results.failure(msg);
    }

    public Result FAILURE(String field, String msg) {
        return Results.failureWithError(field, msg);
    }

    public Result FAILURE(Map<String, Object> errors, String msg) {
        return Results.failureWithError(errors, msg);
    }

    public Result FAILURE(BindingResult result) {
        return this.FAILURE(result.getAllErrors());
    }

    public Result FAILURE(BindingResult result, String msg) {
        return this.FAILURE(result.getAllErrors(), msg);
    }

    public Result FAILURE(List<ObjectError> objectErrors) {
        return this.FAILURE((List)objectErrors, (String)null);
    }

    public Result FAILURE(List<ObjectError> objectErrors, String msg) {
        Map<String, Object> errors = new HashMap();
        if (objectErrors != null && objectErrors.size() > 0) {
            errors = Maps.newHashMap();
            Iterator var4 = objectErrors.iterator();

            while(var4.hasNext()) {
                ObjectError error = (ObjectError)var4.next();
                String key = error instanceof FieldError ? ((FieldError)error).getField() : error.getObjectName();
                errors.put(key, error.getDefaultMessage());
            }
        }

        return Results.failureWithError(errors, msg);
    }

    public String FORWARD(String url) {
        return "forward:" + url;
    }

    public String REDIRECT(String url) {
        return "redirect:" + url;
    }
}
